<?php

class DefaultController extends Controller
{       
        
        //for ftp
        //private $server = '103.28.37.163';
        //private $username = 'img.gomo.vn';
        //private $password = 'CzUp3YFdqvKCMArAlRIU';
        //private $base_url = 'http://img.gomo.vn';
        //private $base_dir = '/public_html/agomo/';
        
        //No ftp
        //private $base_url = 'http://localhost/vqm/admin.gomo.vn';
        private $base_url = 'http://admin.gomo.vn';
        private $base_dir = './agomo/';
        
        
	public function actionIndex()
	{   
            
            if(isset($_FILES['image'])){
                //$path_dir = $_POST['name'].'/'.date('Y').'/'.date('m').'/'.date('d');
                //$fulldir = $this->base_dir.$path_dir;
                $file_name = str_replace(' ','',time().'-'. basename($_FILES['image']['name']));
                //if(!is_dir($fulldir)){
                //        $fulldir = $this->mk_directory($path_dir);
                //}
                
                if (move_uploaded_file($_FILES['image']['tmp_name'], $this->base_dir.$file_name)) { 
                  echo $this->base_url.'/'.'agomo/'.$file_name;
                } else {
                    echo "error";
                }
            }
        }

        
        //mkdir no ftp
        private function mk_directory($path){
            $fullpath = $this->base_dir;
            //echo $fullpath;
            //exit();
            //chmod(substr($fullpath,0,-1), 0777);
            $parts = explode("/",$path);
            foreach($parts as $part){
                if($part != '' ){
                    $fullpath .= $part.'/';
                    if(!is_dir($fullpath)){
                        mkdir($fullpath, 0777,true);
                    }
                }
            }
            if($fullpath == $this->base_dir){
                return false;
            }
            else{
                return $fullpath;
            }
        }
}
        /*
        public function actionIndex()
	{   
            //Upload no ftp
            
            if(isset($_FILES['image'])){
                $path_dir = $_POST['name'].'/'.date('Y').'/'.date('m').'/'.date('d');
                $fulldir = $this->base_dir.$path_dir;
                $file_name = str_replace(' ','',time().'-'. basename($_FILES['image']['name']));
                
                if(!is_dir($fulldir)){
                        $fulldir = $this->mk_directory($path_dir);
                    }
                if (move_uploaded_file($_FILES['image']['tmp_name'], $fulldir.$file_name)) { 
                  echo $this->base_url.'/'.$fulldir.$file_name;
                } else {
                    echo "error";
                }
            }
            
            
            
            
            
            //FTP upload
            /*
            $conn_id = ftp_connect($this->server);
            $login_result = ftp_login($conn_id, $this->username, $this->password);
            if($login_result == 1){  
                if(isset($_FILES['image'])){
                    $path_dir = $_POST['name'].'/'.date('Y').'/'.date('m').'/'.date('d');
                    $fulldir = $this->base_dir.$path_dir;
                    $file_name = str_replace(' ','',time().'-'. basename($_FILES['image']['name']));
                    
                    if(!is_dir('ftp://'.$this->username.':'.$this->password.'@'.$fulldir)){
                        $fulldir = $this->make_directory($conn_id, $path_dir);
                    }
                    $put = ftp_put($conn_id, $fulldir.$file_name, $_FILES['image']['tmp_name'], FTP_BINARY);
                    if($put == 1){
                        echo str_replace('public_html/', '', $this->base_url.$fulldir.$file_name);
                    }else{
                        echo 'false';
                    }
                }
                
                if(isset($_POST['file_name'])){
                    $filename = str_replace($this->base_url, '', $_POST['file_name']);
                    $filename = '/public_html'.$filename;
                    //echo $filename;
                    //exit();
                    if(ftp_delete($conn_id, $filename)){
                        echo 'success';
                    }else{
                        echo 'false';
                    }
                }
                ftp_close($conn_id);
            }
            
            
            
        }
         */

        //mkdir for ftp
        /*
       private function make_directory($conn_id,$path){
            $fullpath = $this->base_dir;
            $parts = explode("/",$path);
            foreach($parts as $part){
                if($part != '' ){
                    $fullpath .= $part.'/';
                    if(!is_dir('ftp://'.$this->username.':'.$this->password.'@'.$this->server.$fullpath)){
                        ftp_mkdir($conn_id, $fullpath);
                    }
                }
            }
            if($fullpath == $this->base_dir){
                return false;
            }
            else{
                return $fullpath;
            }
        }
         * 
         */
        
        
        
        
        
        
        
        
        
            /*
            if(isset($_FILES['image'])){
                $uploaddir = './uploads/images/icons/';
                $file_name_temp = $time.'-'. basename($_FILES['image']['name']);
                $file_name = str_replace(' ','',$file_name_temp);
                $destination = $uploaddir .$file_name; 
                 
                if (move_uploaded_file($_FILES['image']['tmp_name'], $destination)) { 
                  echo 'success';
                } else {
                    echo "error";
                }
            }
            
            if(isset($_FILES['thumb'])){
                $uploaddir = './uploads/images/thumbs/';
                $file_name_temp = $time.'-'. basename($_FILES['thumb']['name']);
                $file_name = str_replace(' ','',$file_name_temp);
                $destination = $uploaddir .$file_name; 
                 
                if (move_uploaded_file($_FILES['thumb']['tmp_name'], $destination)) { 
                  echo 'success';
                } else {
                    echo "error";
                }
            }
            
            if(isset($_FILES['logo'])){
                $uploaddir = './uploads/images/logos/';
                $file_name_temp = $time.'-'. basename($_FILES['logo']['name']);
                $file_name = str_replace(' ','',$file_name_temp);
                $destination = $uploaddir .$file_name; 
                 
                if (move_uploaded_file($_FILES['logo']['tmp_name'], $destination)) { 
                  echo 'success';
                } else {
                    echo "error";
                }
            }
            
            if(isset($_FILES['screenshot'])){
                $uploaddir = './uploads/images/screenshots/';
                $file_name_temp = $time.'-'. basename($_FILES['screenshot']['name']);
                $file_name = str_replace(' ','',$file_name_temp);
                $destination = $uploaddir .$file_name; 
                 
                if (move_uploaded_file($_FILES['screenshot']['tmp_name'], $destination)) { 
                  echo 'success';
                } else {
                    echo "error";
                }
            }
            
	}
             * 
             * 
             */
